package com.bizhibihui.ordermeal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bizhibihui.ordermeal.entity.BizAddress;
import com.bizhibihui.ordermeal.entity.BizFood;
import com.bizhibihui.ordermeal.entity.BizOrder;
import com.bizhibihui.ordermeal.entity.BizOrderFood;
import com.bizhibihui.ordermeal.mapper.BizFoodMapper;
import com.bizhibihui.ordermeal.mapper.BizOrderFoodMapper;
import com.bizhibihui.ordermeal.mapper.BizOrderMapper;
import com.bizhibihui.ordermeal.service.BizOrderService;
import com.bizhibihui.ordermeal.vo.OrderFoodVo;
import com.bizhibihui.ordermeal.vo.OrderVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 */
@Service
@AllArgsConstructor
public class BizOrderServiceImpl extends ServiceImpl<BizOrderMapper, BizOrder> implements BizOrderService {
    BizOrderMapper bizOrderMapper;

    BizFoodMapper bizFoodMapper;

    BizOrderFoodMapper bizOrderFoodMapper;

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

    @Override
    public List<BizOrder> findByCondition(IPage<BizOrder> page, BizOrder bizOrder) {
        List<BizOrder> list = bizOrderMapper.findByCondition(page, bizOrder);
        return list;
    }

    @Override
    public int delete(Integer id) {
        return bizOrderMapper.deleteById(id);
    }

    @Override
    public boolean create(OrderVo orderVo) {
        BizAddress address = orderVo.getAddress();
        List<OrderFoodVo> orderInfo = orderVo.getOrderInfo();
        BizOrder bizOrder = new BizOrder();
        bizOrder.setTradeNo(getBillNo());
        bizOrder.setUpdateTime(new Date());
        bizOrder.setCreateTime(new Date());
        bizOrder.setStatus(1);
        bizOrder.setAddress(address.getDescription());
        bizOrder.setPhone(address.getPhone());
        bizOrder.setUserName(address.getName());
        bizOrder.setUserId(address.getUserId());
        BigDecimal totalMoney = getTotalMoney(orderInfo);
        bizOrder.setMoney(totalMoney);
        int insert = bizOrderMapper.insert(bizOrder);
        if (insert > 0){
            Integer id = bizOrder.getId();
            for (OrderFoodVo orderFoodVo : orderInfo) {
                BizOrderFood orderFood = new BizOrderFood();
                orderFood.setFoodId(orderFoodVo.getId());
                orderFood.setOrderId(id);
                orderFood.setFoodName(orderFoodVo.getName());
                orderFood.setNum(orderFoodVo.getNum());
                orderFood.setTradeNo(bizOrder.getTradeNo());
                orderFood.setUpdateTime(new Date());
                bizOrderFoodMapper.insert(orderFood);
            }
            return true;

        }else{
            return false;
        }

    }

    private BigDecimal getTotalMoney(List<OrderFoodVo> orderInfo){
        BigDecimal totalMoney = BigDecimal.ZERO;
        for (OrderFoodVo orderFoodVo : orderInfo) {
            BigDecimal price = orderFoodVo.getPrice();
            long num = orderFoodVo.getNum();
            BigDecimal multiply = price.multiply(BigDecimal.valueOf(num));
            totalMoney = totalMoney.add(multiply);
        }
        return totalMoney;
    }

    private String getBillNo(){
        String format = simpleDateFormat.format(new Date());
        Random random = new Random();
        int aa = random.nextInt();
        return format+aa;
    }

    @Override
    public int complete(Integer id) {
        BizOrder bizOrder = bizOrderMapper.selectById(id);
        bizOrder.setCompleteTime(new Date());
        bizOrder.setStatus(2);
        bizOrder.setUpdateTime(new Date());
        return bizOrderMapper.updateById(bizOrder);
    }

    @Override
    public int completeBatch(Integer[] ids) {
        int count = 0;
        for (Integer id : ids) {
            int complete = complete(id);
            count = count+complete;
        }
        return count;
    }

    @Override
    public List<BizOrder> getMyOrder(IPage<BizOrder> page, BizOrder bizOrder) {
        List<BizOrder> byCondition = findByCondition(page, bizOrder);
        for (BizOrder order : byCondition) {
            List<BizFood> foodList = bizFoodMapper.getFoodByOrder(order.getId());
            String statusStr = "已下单";
            switch (order.getStatus()){
                case 1:statusStr = "已下单";break;
                case 2:statusStr = "已配送";break;
            }
            order.setStatusStr(statusStr);
            order.setFoodList(foodList);
        }
        return byCondition;
    }
}
